package com.ruoyi.project.cms.product.controller;

import com.ruoyi.common.constant.ProductConstants;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.cms.product.domain.Product;
import com.ruoyi.project.cms.product.domain.ProductPic;
import com.ruoyi.project.cms.product.domain.ProductSeries;
import com.ruoyi.project.cms.product.mapper.PicMapper;
import com.ruoyi.project.cms.product.service.PicService;
import com.ruoyi.project.cms.product.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @CreateTime 2020/5/21 21:14
 */
@Controller
@RequestMapping("/product/pic")
public class ProductPicController extends BaseController {
    private String prefix = "/cms/product/pic";

    @Autowired
    private PicService picService;
    @Autowired
    private ProductService productService;

    @GetMapping("")
    public String menu()
    {
        return prefix+"/productPic";
    }

    /**
     * 主页面菜单显示
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list()
    {
        startPage();
        List<ProductPic> list = picService.findAll();
        return getDataTable(list);
    }

    /**
     * 图片上传
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/picload";
    }

    /**
     * 文件上传
     * @param object
     * @return
     */
    @RequestMapping("/load")
    public String load(ProductPic object){
        System.out.println("先来这里？"+object);
        return "/cms/product/pic/productPic";
    }


    /**
     * 给产品配图
     */
    @GetMapping("/edit/{picId}")
    public String editSeries(@PathVariable("picId") Long picId, ModelMap mmap)
    {
        mmap.put("picShow",picService.findById(picId));
        /*
        * 回头假如有时间把这里的图片查产品优化一下
        * */
        return prefix + "/picshow";
    }
    /**
     * 修改产品图片
     */
    @PostMapping("/picchange")
    @ResponseBody
    public AjaxResult editSaveSeries(@Validated Product product)
    {
        if (product.getPid() == null){
            return error("配图失败，请选择一款产品");
        }
        return toAjax(productService.changeProductPic(product));
    }

    /**
     * 删除图片
     */
//    @RequiresPermissions("system:post:remove")
//    @Log(title = "岗位管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove/{picId}")
    @ResponseBody
    public AjaxResult removeProducts(@PathVariable("picId") Long picId)
    {
        try
        {
            return toAjax(picService.removePicById(picId));
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }
}
